package org.wak.action.actions
{
	import org.wak.action.IInteractable;
	import org.wak.main.PuzzleControllerBase;
	import org.wak.utils.IIterator;
	
	/**
	 * Base class for the Adventure's Actions. 
	 * 
	 */	
	public class BaseAction implements IAction
	{
		protected var mActionId:String = "" ;
		protected var mActionText:String = "";
		
		protected var mParam1:IInteractable = null ;
		
		public function BaseAction()
		{
		}

		/** @inheritDoc		 */		
		public function GetId():String
		{
			return mActionId;
		}
		
		/** @inheritDoc		 */
		public function AddParameter(pItem:IInteractable):void
		{
			mParam1 = pItem ;
		}
		
		/** @inheritDoc		 */
		public function TestParameter(pItem:IInteractable):Boolean
		{
			return true ;
		}
		
		/** @inheritDoc		 */
		public function get isReady():Boolean
		{
			if(mParam1 != null)
			{
				return true ;
			}
			return false ;
		}
		
		/** @inheritDoc		 */
		public function GetText():String
		{
			var fRetText:String = mActionText ;
			
			if( mParam1 != null)
				fRetText += " " + mParam1.interactionName ; 
				
			return fRetText ;
		}
		
		/** @inheritDoc		 */
		public function Execute(pIterator:IIterator):Boolean 
		{
			var fMethodFound:Boolean = false ;
			
			var fPuzzleController:PuzzleControllerBase = null;
			
			var fMethodEventName:String = "" ;
			
			fMethodEventName = mActionId + mParam1.interactableId ;
						
			while( pIterator.hasNext() )
			{
				fPuzzleController = pIterator.next() as PuzzleControllerBase ;
				fMethodFound = fPuzzleController.hasOwnProperty(fMethodEventName) ;
				if( fMethodFound )
				{
					fPuzzleController[fMethodEventName](mParam1);
				}
			}
			
			return fMethodFound
		}
		
		/** @inheritDoc		 */
		public function CanExecute(pIterator:IIterator):Boolean 
		{
			var fMethodFound:Boolean = false ;
			
			var fPuzzleController:PuzzleControllerBase = null;
			
			var fMethodEventName:String = "" ;
			
			fMethodEventName = mActionId + mParam1.interactableId ;
			//trace("Action looking for "+fMethodEventName)
						
			while( pIterator.hasNext() )
			{
				fPuzzleController = pIterator.next() as PuzzleControllerBase ;
				fMethodFound = fMethodFound || fPuzzleController.hasOwnProperty(fMethodEventName) ;
				if( fMethodFound )
				{
					return true ;
				}
			}
			
			return false
		}
		
		/** @inheritDoc		 */
		public function GetNoActionText():String
		{
			return "nah" ;
		} 
		
		/** @inheritDoc		 */
		public function GetParam1():IInteractable 
		{
			return mParam1 ;
		}
		
		/** @inheritDoc		 */
		public function GetParam2():IInteractable 
		{
			return null ;
		}
		
		/** @inheritDoc		 */
		public function get paramsNumber():int
		{
			return 1
		}
		
		
	}
}